{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "norm = tf.random_normal([2, 3], mean= 3.0, stddev= 4.0, seed=1234)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "sess = tf.Session()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5.05361938  1.97674406  5.60796547]\n",
      " [ 8.56945515  4.49027205  3.81345224]]\n"
     ]
    }
   ],
   "source": [
    "print(sess.run(norm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TensorShape([Dimension(1), Dimension(3), Dimension(3), Dimension(7)])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input = tf.Variable(tf.random_normal([1,5,5,5]))  \n",
    "filter = tf.Variable(tf.random_normal([3,3,5,7]))  \n",
    "  \n",
    "op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')\n",
    "op.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "oplist = []\n",
    "input_arg = tf.Variable(tf.ones([1,3,3,5]))\n",
    "filter_arg = tf.Variable(tf.ones([1,1, 5, 1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "op2 = tf.nn.conv2d(input_arg, filter_arg, strides=[1,1,1,1], use_cudnn_on_gpu=False, padding=\"VALID\")\n",
    "oplist.append([op, \"case 2\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[<tf.Tensor 'Conv2D_11:0' shape=(1, 3, 3, 7) dtype=float32>, 'case 2']]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oplist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "v1 = tf.Variable([[1,1],[2,2],[3,3]], name=\"v1\")\n",
    "v2=tf.Variable([[4,4],[5,5],[6,7]],name=\"v2\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_op = tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "saver = tf.train.Saver()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved in file:  ./model/test/model.ckpt-0\n",
      "Model saved in file:  ./model/test/model.ckpt-1\n",
      "Model saved in file:  ./model/test/model.ckpt-2\n",
      "Model saved in file:  ./model/test/model.ckpt-3\n",
      "Model saved in file:  ./model/test/model.ckpt-4\n",
      "Model saved in file:  ./model/test/model.ckpt-5\n",
      "Model saved in file:  ./model/test/model.ckpt-6\n",
      "Model saved in file:  ./model/test/model.ckpt-7\n",
      "Model saved in file:  ./model/test/model.ckpt-8\n",
      "Model saved in file:  ./model/test/model.ckpt-9\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    for step in xrange(10):\n",
    "        sess.run(init_op)\n",
    "        save_path = saver.save(sess, \"./model/test/model.ckpt\", global_step=step)\n",
    "        print \"Model saved in file: \", save_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./model/test/model.ckpt-9\n"
     ]
    }
   ],
   "source": [
    "save_ = saver.last_checkpoints[-1]\n",
    "print save_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = tf.placeholder(tf.float32, shape=[None, 1])\n",
    "y = 4 * x + 4\n",
    "\n",
    "w = tf.Variable(tf.random_normal([1], -1, 1))\n",
    "b = tf.Variable(tf.zeros([1]))\n",
    "y_predict = w * x + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss = tf.reduce_mean(tf.square(y - y_predict))\n",
    "optimizer = tf.train.GradientDescentOptimizer(0.5)\n",
    "train = optimizer.minimize(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "isTrain = False\n",
    "train_step = 2000\n",
    "checkpoint_steps = 50\n",
    "checkpoint_dir = \"./model/test2/\"\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "saver = tf.train.Saver()\n",
    "x_data = np.reshape(np.random.rand(10).astype(np.float32), (10,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model_checkpoint_path: \"./model/test2/model.ckpt-2000\"\n",
      "all_model_checkpoint_paths: \"./model/test2/model.ckpt-1800\"\n",
      "all_model_checkpoint_paths: \"./model/test2/model.ckpt-1850\"\n",
      "all_model_checkpoint_paths: \"./model/test2/model.ckpt-1900\"\n",
      "all_model_checkpoint_paths: \"./model/test2/model.ckpt-1950\"\n",
      "all_model_checkpoint_paths: \"./model/test2/model.ckpt-2000\"\n",
      "\n",
      "INFO:tensorflow:Restoring parameters from ./model/test2/model.ckpt-2000\n",
      "[ 3.99999714]\n",
      "[ 4.00000143]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    sess.run(tf.initialize_all_variables())\n",
    "    if isTrain:\n",
    "        for i in xrange(train_step):\n",
    "            sess.run(train, feed_dict={x: x_data})\n",
    "            if (i+1) % checkpoint_steps == 0:\n",
    "                saver.save(sess,  checkpoint_dir +\"model.ckpt\", global_step= i +1)\n",
    "    else:\n",
    "        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)\n",
    "        print ckpt\n",
    "        if ckpt and ckpt.model_checkpoint_path:\n",
    "            saver.restore(sess, ckpt.model_checkpoint_path)\n",
    "        else:\n",
    "            pass\n",
    "        print (sess.run(w))\n",
    "        print(sess.run(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "u'./model/test2/model.ckpt-2000'"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " ckpt and ckpt.model_checkpoint_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 5 7]\n",
      "[1 2 3]\n",
      "[2 3 4]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "a = tf.constant([1,2,3], name=\"a\")\n",
    "b = tf.constant([2,3,4], name=\"b\")\n",
    "\n",
    "y = a + b\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    \n",
    "    output = sess.run(y)\n",
    "    print output\n",
    "    print sess.run(a)\n",
    "    print sess.run(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "learning = 0.01\n",
    "batch_size = 16\n",
    "epoch_step = 10000\n",
    "display_step = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = tf.placeholder(\"float\", [None, 784])\n",
    "y = tf.placeholder(\"float\", [None, 10])\n",
    "\n",
    "layer1 = 16\n",
    "layer2 = 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "w = {\n",
    "    \"h1\": tf.Variable(tf.random_normal([784, layer1])),\n",
    "    \"h2\": tf.Variable(tf.random_normal([layer1, layer2])),\n",
    "    \"out\": tf.Variable(tf.random_normal([layer2, 10]))\n",
    "}\n",
    "\n",
    "b = {\n",
    "    \"h1\": tf.Variable(tf.random_normal([layer1])),\n",
    "    \"h2\": tf.Variable(tf.random_normal([layer2])),\n",
    "    \"out\": tf.Variable(tf.random_normal([10]))\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def network(x_input, weight, biases):\n",
    "    net1 = tf.nn.relu(tf.matmul(x_input, weight[\"h1\"]) + biases[\"h1\"])\n",
    "    net2 = tf.nn.relu(tf.matmul(net1, weight[\"h2\"]) + biases[\"h2\"])\n",
    "    out =tf.matmul(net2, weight[\"out\"]) + biases[\"out\"]\n",
    "    return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = network(x, w, b)\n",
    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred))\n",
    "optimizer = tf.train.AdamOptimizer(learning_rate= learning).minimize(cost)\n",
    "\n",
    "correct_pred = tf.equal(tf.argmax(y, 1), tf.argmax(pred, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_pred,\"float\"))\n",
    "\n",
    "init = tf.global_variables_initializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "__exit__",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-165-4b74e128cb42>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSession\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0msess\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m     \u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0;32mfor\u001b[0m \u001b[0mepoch\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mepoch_step\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m         \u001b[0mavg_cost\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: __exit__"
     ]
    }
   ],
   "source": [
    "with tf.Session as sess:\n",
    "    sess.run(init)\n",
    "    \n",
    "    for epoch in range(epoch_step):\n",
    "        avg_cost = 0\n",
    "        total_batch = int(alldata / batch_size)\n",
    "        for i in range(total_batch):\n",
    "            x_batch, y_batch = mnist.train.next_batch(100) # batch_size的输入，对应的输入的标签\n",
    "            _, output = sess.run([optimizer, cost], feed_dict={x:x_batch, y: y_batch})\n",
    "            avg_cost +=output / total_batch\n",
    "        \n",
    "        if epoch % display_step == 0:\n",
    "            print(\"cost: \", avg_cost)\n",
    "    print(\"finish!\")\n",
    "    print(\"accuracy: \", sess.run(accuracy, feed_dict = {x: test_x, y: test_y}))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'test_x' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-168-f26a7d4d748a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"accuracy: \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maccuracy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mtest_x\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mtest_y\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'test_x' is not defined"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
